<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search"  @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>

      <el-table :data="goodList" style="width: 100%">
         <el-table-column type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格（元）" width="100"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量"  width="100"></el-table-column>
        <el-table-column prop="add_time" label="创建时间"  width="180">
          <template slot-scope="scope">
            {{scope.row.add_time|dataFormat}}
          </template>

        </el-table-column>
        <el-table-column  label="操作"  width="180">
          <template slot-scope="scope">
              <el-button type="primary" class="el-icon-edit" size="mini">编辑</el-button>
            <el-button type="danger" class="el-icon-delete" size="mini" @click="removeById(scope.row.goods_id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>


      <!-- 分页功能 -->
       <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
  </div>
</template>
           
<script>
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      goodList: [],
      total: 0
    };
  },
  methods: {
    async getGoodsList() {
      let { data: res } = await this.axios.get('goods', {
        params: this.queryInfo
      });
      // console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error('请求发生错误');
      }
      this.goodList = res.data.goods;
      this.total = res.data.total;
      this.queryInfo.pagenum =Number(res.data.pagenum);
      
    },
     handleSizeChange(val) {
        // console.log(`每页 ${val} 条`);
        this.queryInfo.pagesize=val;
        this.getGoodsList()
      },
      handleCurrentChange(val) {
        // console.log(`当前页: ${val}`);
        this.queryInfo.pagenum=val;
         this.getGoodsList()
      },
      async removeById(id){
       let {data:res}=await this.axios.delete(`goods/${id}`) 
       console.log(res);
       if(res.meta.status!==200){
         return this.$message.error('删除失败')
       }
       this.$message.success('删除成功')
       
      },
      goAddPage(){
        this.$router.push('/goods/add')
      }
  },
  created() {
    this.getGoodsList();
  }
};
</script>
<style scoped lang="less">
.el-card {
  margin-top: 15px;
}
</style>